package name.vaccari.matteo.tai.phonedirectory.views;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import name.vaccari.matteo.tai.phonedirectory.web.BaseViewTestCase;

import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;

import freemarker.template.Configuration;
import freemarker.template.ObjectWrapper;
import freemarker.template.Template;


public class FreemarkerTest extends BaseViewTestCase {

	private Configuration configuration;

	@Before
	public void setUp() throws Exception {
		configuration = new Configuration();
		configuration.setDirectoryForTemplateLoading(new File("templates"));
	}
	
	@Test
	public void testFreemarker() throws Exception {
		String templateString = "Hello, ${user}";
		Template template = new Template("my template", new StringReader(templateString), configuration);
		
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("user", "Tizius");
		
		StringWriter writer = new StringWriter();
		template.process(model, writer);
		assertEquals("Hello, Tizius", writer.toString());
	}
	
    @Test
    public void testXpath() throws Exception {
        Document doc = documentFromString("<a><b><c>foo</c></b></a>");
        assertEquals(0, getNodeList(doc, "//foo").getLength());
        assertEquals(1, getNodeList(doc, "//c").getLength());
        assertEquals("foo", getNodeContent(doc, "/a/b/c"));
    }
    
    public class GreenMouse {
		public String getUrl() {
			return "greenmouse.html";
		}
		public String getName() {
			return "green mouse";
		}
    }

    @Test
    public void bigTest() throws Exception {
        Template template = configuration.getTemplate("bigjoe.ftl");

        Map model = new HashMap();
        model.put("user", "Big Joe");
        model.put("latestProduct", new GreenMouse());

        StringWriter writer = new StringWriter();
        template.process(model, writer);

        String expected = "<html>\n" +
        "<head>\n" +
        "  <title>Welcome!</title>\n" +
        "</head>\n" +
        "<body>\n" +
        "  <h1>Welcome Big Joe!</h1>\n" +
        "  <p>Our latest product:\n" +
        "  <a href=\"greenmouse.html\">green mouse</a>!\n" +
        "  </p>\n" +
        "</body>\n" +
        "</html>\n";
        
        assertEquals(expected, writer.toString());
    }
    
    @Test
    public void betterTest() throws Exception {
        Template template = configuration.getTemplate("bigjoe.ftl");

        Map model = new HashMap();
        model.put("user", "Big Joe");
        model.put("latestProduct", new GreenMouse());

        StringWriter writer = new StringWriter();
        template.process(model, writer);

        Document document = documentFromString(writer.toString());
        assertEquals("Welcome!", getNodeContent(document, "/html/head/title"));
        assertEquals("green mouse", getNodeContent(document, "//a[@href='greenmouse.html']"));
    }

}
